import pytest
import allure
from pages.page import HomePage, CustomerList, LoginInformation, InvoiceCollection, Accountant
from time import sleep


@allure.epic("票账税6.0")
class TestInvoiceTax:
    @allure.feature("客户管理")
    @allure.story("客户列表")
    @allure.title("新增企业")
    @pytest.mark.parametrize("company", [
        # "湖北地区自动化测试",
        # "浙江地区自动化测试",
        "天津地区自动化测试"
        # "广东地区自动化测试"
    ])
    def test_add(self, user_driver, company):
        with allure.step("关闭弹窗"):
            HomePage(user_driver).pop_up_window_manage()
        # with allure.step("关闭‘我知道了’小提示"):
        #     HomePage(user_driver).tip_manage()
        with allure.step("选择测试圈子"):
            HomePage(user_driver).select_circle()
        with allure.step("关闭弹窗"):
            HomePage(user_driver).pop_up_window_manage()
        with allure.step("进入客户列表页面"):
            HomePage(user_driver).enter_customer_list()
        with allure.step("关闭'通过账无忧下单快递'小提示"):
            CustomerList(user_driver).tip_manage()
        with allure.step("新增客户"):
            CustomerList(user_driver).add_customer(company)
        with allure.step("断言"):
            assert "新增客户成功" == CustomerList(user_driver).add_customer_alert()

    @allure.feature("记账报税")
    @allure.story("票税登录信息")
    @allure.title("税局验证")
    @pytest.mark.parametrize("company, taxpayer_id, account, pwd", [
        # ["湖北地区自动化测试", "91441403MA51DML791", "18825082466", "Aa123456"],
        # ["浙江地区自动化测试", "91330703MABMKA5Y31", "15024388477", "zxm112233"],
        ["天津地区自动化测试", "91120222MA06DTHB0P", "130323199912291025", "cz123456"]
        # ["广东地区自动化测试", "91420116MA49G9H456", "15926368658", "Zrfty198069@"]
    ])
    def test_certify_customer(self, user_driver, company, taxpayer_id, account, pwd):
        with allure.step("进入票税登录信息页面"):
            HomePage(user_driver).default_content()
            HomePage(user_driver).enter_login_information()
        with allure.step("输入企业名称并搜索"):
            LoginInformation(user_driver).enter_login_information_iframe()
            LoginInformation(user_driver).input_select(company)
        with allure.step("点击客户名称进入登录信息页面"):
            LoginInformation(user_driver).click_company_name()
        with allure.step("输入纳税人识别号"):
            LoginInformation(user_driver).input_taxpayer_id_number(taxpayer_id)
        with allure.step("输入账号"):
            LoginInformation(user_driver).input_account(account)
        with allure.step("输入密码"):
            LoginInformation(user_driver).input_password(pwd)
        with allure.step("立即验证"):
            LoginInformation(user_driver).click_certify()
        with allure.step("关闭"):
            LoginInformation(user_driver).close()
        with allure.step("刷新判断验证状态"):
            count = 1
            while count < 10:
                count += 1
                sleep(10)
                LoginInformation(user_driver).refresh()
                status = LoginInformation(user_driver).certify_status()
                if status == "验证中":
                    continue
                elif status == "验证成功":
                    break
                else:
                    break
        with allure.step("断言"):
            assert "验证成功" == LoginInformation(user_driver).certify_status()

    # @allure.story("跳转税局")
    # @pytest.mark.parametrize("company, title", [
    #     ["广东云取票自动化测试", "税务局"]
    # ])
    # def test_enter_tax_bureau(self, user_driver, company, title):
    #     with allure.step("输入客户名称并搜索"):
    #         LoginInformation(user_driver).input_select(company)
    #     with allure.step("点击'登录税局'跳转进入税局"):
    #         current_window = InvoiceCollection(user_driver).current_window()
    #         LoginInformation(user_driver).enter_tax_bureau()
    #     with allure.step("跳转页面"):
    #         LoginInformation(user_driver).switch_to_window(title)
    #     with allure.step("断言"):
    #         assert "欢迎" in LoginInformation(user_driver).tax_user_loc
    #     with allure.step("返回主页面"):
    #         LoginInformation(user_driver).switch_to_current_window(current_window)

    @allure.feature("记账报税")
    @allure.story("发票采集")
    @allure.title("会计页面发票采集")
    @pytest.mark.parametrize("company_name, title", [
        # ["广东地区测试企业228", "金蝶在线会计"],
        ["广东云取票自动化测试", "金蝶在线会计"]
    ])
    def test_account_book_collect(self, user_driver, company_name, title):
        with allure.step("进入发票采集页面"):
            HomePage(user_driver).switch_to_default_content()
            HomePage(user_driver).enter_invoice_collection()
        with allure.step("输入客户名称并搜索"):
            InvoiceCollection(user_driver).input_company_search(company_name)
        with allure.step("进账簿"):
            current_window = InvoiceCollection(user_driver).current_window()
            InvoiceCollection(user_driver).enter_account_book()
        with allure.step("跳转页面"):
            Accountant(user_driver).switch_to_window(title)
        with allure.step("进项票"):
            Accountant(user_driver).enter_income_invoice()
        with allure.step("智能采集"):
            Accountant(user_driver).initiate_income_invoice()
        with allure.step("断言"):
            Accountant(user_driver).income_invoice_result()
        with allure.step("返回主页面"):
            Accountant(user_driver).switch_to_current_window(current_window)

    @allure.feature("客户管理")
    @allure.story("客户列表")
    @allure.title("删除企业数据")
    @pytest.mark.parametrize("company", [
        # "湖北地区自动化测试",
        # "浙江地区自动化测试",
        "天津地区自动化测试"
        # "广东地区自动化测试"
    ])
    def test_delete_customer(self, user_driver, company):
        with allure.step("进入客户列表页面"):
            HomePage(user_driver).default_content()
            HomePage(user_driver).enter_customer_list()
        with allure.step("删除企业数据"):
            CustomerList(user_driver).delete_customer(company)
        with allure.step("断言"):
            assert "删除客户成功" == CustomerList(user_driver).delete_customer_alert()
